<template>
  <div>
    <input v-model="newTodoTitle" placeholder="添加待办事项" />
    <button @click="addTodo">添加</button>
    <div v-for="(todo, index) in todos" :key="index">
      <TodoItem
        :title="todo.title"
        :completed="todo.completed"
        @update:completed="updateTodoCompleted($event, index)"
        @remove-todo="removeTodo(index)"
      />
    </div>
  </div>
</template>

<script>
import TodoItem from "./TodoItem.vue";

export default {
  components: {
    TodoItem,
  },
  data() {
    return {
      todos: [],
      newTodoTitle: "",
    };
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = "";
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(completed, index) {
      this.todos[index].completed = completed;
    },
  },
};
</script>

